<UserControl x:Class="LogGrok.IndexedFilter.IndexedFilterView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:be="clr-namespace:LogGrok.Infrastructure.Behaviors;assembly=LogGrok.Infrastructure"
             xmlns:behaviors="clr-namespace:LogGrok.IndexedFilter.Behaviors"
             xmlns:commands="clr-namespace:LogGrok.IndexedFilter.Commands"
             xmlns:controls="clr-namespace:LogGrok.Controls;assembly=LogGrok.Controls"
             xmlns:local="clr-namespace:LogGrok.IndexedFilter"
             xmlns:metroControls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
             xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/LogGrok.Controls;component/Styles/ToggleButtonStyle.xaml" />
                <ResourceDictionary Source="/LogGrok.Controls;component/Resources/Icons.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
            </ResourceDictionary.MergedDictionaries>
            <Path x:Key="FilterPath"
                  Width="Auto"
                  Height="Auto"
                  Stretch="Uniform"
                  x:Shared="True">
                <Path.Fill>
                    <VisualBrush Visual="{StaticResource appbar_filter}" />
                </Path.Fill>
            </Path>
            <DataTemplate x:Key="CategoriesPopup">
                <controls:ResizablePopup Width="250"
                                         Height="300"
                                         MinWidth="200"
                                         MinHeight="200"
                                         StaysOpen="False"
                                         be:BindPopupBehavior.ToToggleButton="{Binding ElementName=CategoriesSearchButton}"
                                         be:RoutedCommandBehavior.RoutedCommandToCommandBinding="{be:RoutedCommandToCommandBinding Command={Binding Source={x:Static commands:PopupCommands.Close}},
                                                                                                                                   RoutedCommand={x:Static commands:RoutedCommands.Cancel}}"
                                         behaviors:FocusOnOpenPopupBehavior.DefaultFocusedElement="{Binding ElementName=FilterTextBox}">
                    <controls:ResizablePopup.InputBindings>
                        <KeyBinding Key="Escape"
                                    Command="{x:Static commands:RoutedCommands.Cancel}"
                                    CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=Popup}}" />
                    </controls:ResizablePopup.InputBindings>
                    <Border BorderBrush="LightGray" BorderThickness="1">
                        <Grid Background="White">
                            <AdornerDecorator>
                                <Grid Margin="4">
                                    <Grid.Resources>
                                        <CollectionViewSource x:Key="items" Source="{Binding Elements}">
                                            <CollectionViewSource.SortDescriptions>
                                                <scm:SortDescription PropertyName="Category" />
                                            </CollectionViewSource.SortDescriptions>
                                        </CollectionViewSource>
                                    </Grid.Resources>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition />
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition Width="*" />
                                    </Grid.ColumnDefinitions>
                                    <TextBox x:Name="FilterTextBox"
                                             Grid.ColumnSpan="3"
                                             Margin="10, 10, 10, 0"
                                             Text="{Binding TextFilter,
                                                            UpdateSourceTrigger=PropertyChanged}"
                                             be:RoutedCommandBehavior.RoutedCommandToCommandBinding="{be:RoutedCommandToCommandBinding Command={Binding Source={x:Static commands:TextBoxCommands.Clear}},
                                                                                                                                       RoutedCommand={x:Static commands:RoutedCommands.Cancel}}"
                                             
                                             metroControls:TextBoxHelper.ButtonCommand="{Binding Source={x:Static commands:TextBoxCommands.Clear}}"
                                             metroControls:TextBoxHelper.ButtonCommandParameter="{Binding ElementName=FilterTextBox}"
                                             metroControls:TextBoxHelper.SelectAllOnFocus="True"
                                             metroControls:TextBoxHelper.Watermark="Search...">
                                        <TextBox.InputBindings>
                                            <KeyBinding Key="Escape"
                                                        Command="{x:Static commands:RoutedCommands.Cancel}"
                                                        CommandParameter="{Binding ElementName=FilterTextBox}" />
                                        </TextBox.InputBindings>
                                        <TextBox.Style>
                                            <Style BasedOn="{StaticResource MetroTextBox}" TargetType="{x:Type TextBox}">
                                                <Setter Property="metroControls:TextBoxHelper.ClearTextButton" Value="True" />
                                                <Style.Triggers>
                                                    <Trigger Property="metroControls:TextBoxHelper.HasText" Value="False">
                                                        <Setter Property="metroControls:TextBoxHelper.ClearTextButton" Value="False" />
                                                    </Trigger>
                                                    <Trigger Property="metroControls:TextBoxHelper.HasText" Value="True">
                                                        <Setter Property="metroControls:TextBoxHelper.ClearTextButton" Value="True" />
                                                    </Trigger>
                                                </Style.Triggers>
                                            </Style>
                                        </TextBox.Style>
                                    </TextBox>
                                    <Button Grid.Row="1"
                                            Margin="10, 10, 5, 0"
                                            Command="{Binding SelectAll}"
                                            CommandParameter="{Binding Items,
                                                                       ElementName=ListBox}"
                                            Content="Select All" />
                                    <Button Grid.Row="1"
                                            Grid.Column="1"
                                            Margin="10, 10, 5, 0"
                                            Command="{Binding DeselectAll}"
                                            CommandParameter="{Binding Items,
                                                                       ElementName=ListBox}"
                                            Content="Deselect All" />
                                    <Button Grid.Row="2"
                                            Grid.ColumnSpan="2"
                                            Margin="10, 10, 5, 0"
                                            Command="{Binding SelectOnlySearchResults}"
                                            CommandParameter="{Binding Items,
                                                                       ElementName=ListBox}"
                                            Content="Select Only Search Results" />
                                    <ListBox x:Name="ListBox"
                                             Grid.Row="3"
                                             Grid.ColumnSpan="4"
                                             Margin="10"
                                             HorizontalContentAlignment="Stretch"
                                             ItemsSource="{Binding Source={StaticResource items}}"
                                             SelectionMode="Single"
                                             behaviors:ItemsFilteringBehavior.FilteredProperty="Name"
                                             behaviors:ItemsFilteringBehavior.TextFilter="{Binding TextFilter}">
                                        <ListBox.ItemTemplate>
                                            <DataTemplate>
                                                <ContentControl Content="{Binding}">
                                                    <ContentControl.Resources>
                                                        <DataTemplate DataType="{x:Type local:ElementViewModel}">
                                                            <Grid>
                                                                <Grid.ColumnDefinitions>
                                                                    <ColumnDefinition Width="*" />
                                                                    <ColumnDefinition Width="*" />
                                                                </Grid.ColumnDefinitions>
                                                                <CheckBox Command="{Binding Path=DataContext.RefreshActiveChanged,
                                                                                            ElementName=ListBox}"
                                                                          Content="{Binding Name}"
                                                                          IsChecked="{Binding IsActive}" />
                                                                <TextBlock Grid.Column="1"
                                                                           Margin="0,0,5,0"
                                                                           HorizontalAlignment="Right"
                                                                           FontStyle="Italic"
                                                                           Foreground="Gray"
                                                                           Text="{Binding Percent}" />
                                                            </Grid>
                                                        </DataTemplate>
                                                        <!--
                                                            <DataTemplate DataType="{x:Type local:ComponentViewModel}">
                                                            <CheckBox Command="{Binding Path=DataContext.RefreshActiveChanged,
                                                            ElementName=ListBox}"
                                                            Content="{Binding Name}"
                                                            IsChecked="{Binding IsActive}" />
                                                            </DataTemplate>
                                                            <DataTemplate >
                                                            <CheckBox Command="{Binding Path=DataContext.RefreshActiveChanged,
                                                            ElementName=ListBox}"
                                                            Content="{Binding Name}"
                                                            IsChecked="{Binding IsActive}" />
                                                            </DataTemplate>
                                                            <DataTemplate >
                                                            <Grid>
                                                            <Grid.ColumnDefinitions>
                                                            <ColumnDefinition Width="*" />
                                                            <ColumnDefinition Width="*" />
                                                            </Grid.ColumnDefinitions>
                                                            <CheckBox Command="{Binding Path=DataContext.RefreshActiveChanged,
                                                            ElementName=ListBox}"
                                                            Content="{Binding Name}"
                                                            IsChecked="{Binding IsActive}" />
                                                            <TextBlock Grid.Column="1"
                                                            Margin="0,0,5,0"
                                                            HorizontalAlignment="Right"
                                                            FontStyle="Italic"
                                                            Foreground="Gray"
                                                            Text="{Binding Percent}" />
                                                            </Grid>
                                                            </DataTemplate>
                                                        -->
                                                    </ContentControl.Resources>
                                                </ContentControl>
                                            </DataTemplate>
                                        </ListBox.ItemTemplate>
                                    </ListBox>
                                </Grid>
                            </AdornerDecorator>
                        </Grid>
                    </Border>
                </controls:ResizablePopup>
            </DataTemplate>
        </ResourceDictionary>
    </UserControl.Resources>
    <Grid>
        <ToggleButton x:Name="CategoriesSearchButton"
                      Margin="2"
                      Background="Transparent">
            <ToggleButton.Style>
                <Style BasedOn="{StaticResource SearchOptionToggleButtonStyle}" TargetType="ToggleButton">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsFilterApplied}" Value="False">
                            <Setter Property="BorderThickness" Value="0" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding IsFilterApplied}" Value="True">
                            <Setter Property="BorderBrush" Value="{StaticResource ToggleButtonGoldBorder}" />
                            <Setter Property="BorderThickness" Value="1" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ToggleButton.Style>
            <ToggleButton.Content>
                <Grid>
                    <ContentControl>
                        <Rectangle Width="14"
                                   Height="14"
                                   Fill="{Binding RelativeSource={RelativeSource AncestorType=ToggleButton},
                                                  Path=Foreground}">
                            <Rectangle.OpacityMask>
                                <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_filter}" />
                            </Rectangle.OpacityMask>
                        </Rectangle>
                    </ContentControl>
                    <ContentControl Content="{Binding}" ContentTemplate="{StaticResource CategoriesPopup}" />
                </Grid>
            </ToggleButton.Content>
        </ToggleButton>
    </Grid>
</UserControl>
